Skip to content

fix: early return in toolbar button to avoid unneeded calls to useInt…#24

Open
Arukuen wants to merge 2 commits intodevelopfrom
fix/23-kadence-tabs
Open

fix: early return in toolbar button to avoid unneeded calls to useInt…#24
Arukuen wants to merge 2 commits intodevelopfrom
fix/23-kadence-tabs

Conversation

@Arukuen
Copy link
Copy Markdown
Contributor

@Arukuen Arukuen commented May 5, 2026

…eractions

fixes #23

Summary by CodeRabbit

  • Refactor
    • Optimized internal hook implementation for improved code organization and maintainability.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 5, 2026

📝 Walkthrough

Walkthrough

The useInteractions hook refactors its derived state computation. A dedicated useSelect selector now filters interactions using isInteractionShown instead of filtering inline, and the return object spreads the base data to reduce redundancy.

Changes

State Selection Refactoring

Layer / File(s) Summary
Selector Extraction
src/editor/hooks/use-interactions.js
A new useSelect is introduced to derive interactionsFiltered by filtering data.interactions with isInteractionShown. This selector recomputes only when data changes.
Return Value Restructuring
src/editor/hooks/use-interactions.js
The hook return simplifies to include loadingError and interactionsFiltered, then spreads ...data to export the already-selected interactions and mutation methods instead of manually listing them.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes


🐰 The selector hops with grace,
No more filters in the base,
Spread the data, clean and neat,
Tab selection, now complete!
Hopping toward a stable beat. 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title is truncated and unclear, stating 'fix: early return in toolbar button to avoid unneeded calls to useInt…' which does not fully convey the actual change of refactoring interactionsFiltered selection logic. Provide the complete PR title that accurately describes the refactoring of interactionsFiltered selection logic to resolve the Kadence inspector tab reset issue.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed The PR refactors selection logic to reduce re-renders by isolating interactionsFiltered into a separate useSelect, directly addressing the Kadence inspector tab reset issue (#23).
Out of Scope Changes check ✅ Passed All changes in useInteractions.js are directly related to isolating interactionsFiltered selection logic to reduce re-renders and fix the Kadence inspector tab reset issue.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/23-kadence-tabs

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

🤖 Pull request artifacts

file commit
pr24-interactions-24-merge.zip 8ca2974

github-actions Bot added a commit that referenced this pull request May 5, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/editor/plugins/block-toolbar-button/index.js (1)

65-65: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Typo: "Reeset" → "Reset".

-		// Reeset the popover mode when the popover closes.
+		// Reset the popover mode when the popover closes.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/editor/plugins/block-toolbar-button/index.js` at line 65, Fix the typo in
the comment inside src/editor/plugins/block-toolbar-button/index.js: change
"Reeset the popover mode when the popover closes." to "Reset the popover mode
when the popover closes." so the inline comment above the popover-handling code
(in the block-toolbar-button module) reads correctly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/editor/plugins/block-toolbar-button/index.js`:
- Around line 36-46: The conditional early-return after computing
hasAnchorAttribute causes hooks (useInteractions, useBlockHasInteraction,
useWithShift, useDispatch, the extra useState and useEffect calls) to be called
only when hasAnchorAttribute is true which breaks the Rules of Hooks; fix by
extracting the hook-using logic into a new inner component (e.g.,
BlockToolbarButtonInner) that accepts props and uses
isPopoverOpen/setIsPopoverOpen, useInteractions, useBlockHasInteraction,
useWithShift, useDispatch, and the useEffect hooks; keep the outer component to
only compute hasAnchorAttribute (using useSelect) and then return either
<BlockEdit {...props} /> when !hasAnchorAttribute or <BlockToolbarButtonInner
{...props} /> when true so hooks are called unconditionally within the inner
component; also correct the typo "Reeset" to "Reset".

---

Outside diff comments:
In `@src/editor/plugins/block-toolbar-button/index.js`:
- Line 65: Fix the typo in the comment inside
src/editor/plugins/block-toolbar-button/index.js: change "Reeset the popover
mode when the popover closes." to "Reset the popover mode when the popover
closes." so the inline comment above the popover-handling code (in the
block-toolbar-button module) reads correctly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b0d831ea-f083-4123-a16a-e30edce5c4c5

📥 Commits

Reviewing files that changed from the base of the PR and between eacb69f and bbd7e8e.

📒 Files selected for processing (1)
  • src/editor/plugins/block-toolbar-button/index.js

Comment thread src/editor/plugins/block-toolbar-button/index.js Outdated
Copy link
Copy Markdown
Collaborator

@bfintal bfintal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue seems to be with the useInteractions hook, so let's fix the hook itself instead of returning early to avoid using the hook

…ditor re-renders and prevent Kadence inspector tab reset
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/editor/hooks/use-interactions.js`:
- Around line 256-258: The selector passed to useSelect (used to compute
interactionsFiltered) currently depends on the whole data object ([data])
causing unnecessary re-runs; change the dependency array to [data.interactions]
so the selector only re-evaluates when interactions actually change. Locate the
useSelect call that defines interactionsFiltered and update its dependency array
accordingly (the callback uses isInteractionShown and data.interactions).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5401ae40-e7b7-410f-b1db-4b7be1448380

📥 Commits

Reviewing files that changed from the base of the PR and between bbd7e8e and 8ca2974.

📒 Files selected for processing (1)
  • src/editor/hooks/use-interactions.js

Comment on lines +256 to +258
const interactionsFiltered = useSelect( select => {
return data.interactions.filter( interaction => isInteractionShown( interaction, select ) )
}, [ data ] )
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🌐 Web query:

In @wordpress/datauseSelect, does putting an object like[data]in deps cause recalculation when object identity changes, and is[data.interactions] the recommended dependency when only that field is used?

💡 Result:

Yes, putting an object like [data] in the useSelect dependencies array causes recalculation (i.e., the mapSelect callback is rememoized via useCallback) whenever the object's reference changes, even if its contents are shallowly equal. This is because the dependencies array uses React's standard dependency comparison, which for objects is reference equality (!==), not shallow content equality.[1][2][3] The deps array memoizes the mapSelect function with useCallback(mapSelect, deps), so a new object reference [data] (if data is a new object instance) will trigger recreation of the callback function, causing useSelect to re-evaluate selectors even if store state hasn't changed.[2][4] [data.interactions] is the recommended approach when only that field is used inside the mapSelect callback. Official guidance emphasizes including in deps exactly the values referenced inside the callback (like primitive IDs, search terms, or specific fields), to ensure correctness (callback uses current values) and performance (avoids unnecessary recomputations).[5][1][6][7][3] Using a primitive or specific field avoids unnecessary triggers from unrelated object reference changes. ESLint react-hooks/exhaustive-deps is configured to validate useSelect deps.[4]

Citations:


🏁 Script executed:

# Read the file around the mentioned lines to understand the context
sed -n '240,275p' src/editor/hooks/use-interactions.js

Repository: gambitph/Interactions

Length of output: 1220


🏁 Script executed:

# Check the full function to see the first useSelect that creates `data`
sed -n '200,260p' src/editor/hooks/use-interactions.js | head -70

Repository: gambitph/Interactions

Length of output: 2027


Narrow the selector dependency to data.interactions to avoid unnecessary re-evaluations.

At line 258, using [ data ] as a dependency makes this selector re-evaluate whenever the entire data object reference changes. Since this callback only accesses data.interactions, the dependency array should be narrowed to [ data.interactions ]. This follows the WordPress @wordpress/data guidance and ensures the selector runs only when the interactions data actually changes.

Suggested fix
	const interactionsFiltered = useSelect( select => {
		return data.interactions.filter( interaction => isInteractionShown( interaction, select ) )
-	}, [ data ] )
+	}, [ data.interactions ] )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const interactionsFiltered = useSelect( select => {
return data.interactions.filter( interaction => isInteractionShown( interaction, select ) )
}, [ data ] )
const interactionsFiltered = useSelect( select => {
return data.interactions.filter( interaction => isInteractionShown( interaction, select ) )
}, [ data.interactions ] )
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/editor/hooks/use-interactions.js` around lines 256 - 258, The selector
passed to useSelect (used to compute interactionsFiltered) currently depends on
the whole data object ([data]) causing unnecessary re-runs; change the
dependency array to [data.interactions] so the selector only re-evaluates when
interactions actually change. Locate the useSelect call that defines
interactionsFiltered and update its dependency array accordingly (the callback
uses isInteractionShown and data.interactions).

github-actions Bot added a commit that referenced this pull request May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kadence Blocks compatibility - Active tab resets to general when interactions is enabled

2 participants